﻿<?PHP
require_once("base.wpfb.php");
require_once ('src/facebook.php');

class ViewDatabase{

private $globalVars = null;
private $entryPageName = null;

public function __construct($pageParent) {
	$this->globalVars = new WPFB(plugin_dir_url(__FILE__));
	$this->entryPageName = $pageParent;
}

public function top500Post()
{
	try{
		$databaseRowsAsTR = null;
		if( isset($_GET['show_group_id']) )
		{
			if( !is_numeric($_GET['show_group_id']) )
				throw new Exception('Group id must be numeric only');
			
			if( isset($_GET['sort_order']) &&  $_GET['sort_order'] == 'like_count')
				$databaseRowsAsTR = $this->showGroupInformationListFromDatabase($_GET['show_group_id'],'like_count');
			else if( isset($_GET['sort_order']) &&  $_GET['sort_order'] == 'message_creation_time')
				$databaseRowsAsTR = $this->showGroupInformationListFromDatabase($_GET['show_group_id'],'message_creation_time');
			else if( isset($_GET['sort_order']) &&  $_GET['sort_order'] == 'weekly')
			{
				if( !is_numeric($_GET['weekNo']) || !($_GET['weekNo']>=1 && $_GET['weekNo']<=53) )
					throw new Exception('Week number must be numeric and between 1~53.');
				$databaseRowsAsTR = $this->getWeeklyDataFromDatabase($_GET['show_group_id'],$_GET['weekNo']);
			}
			else
				$databaseRowsAsTR = $this->showGroupInformationListFromDatabase($_GET['show_group_id'],'all');
		}
		$groupList = null;
		$groupList = $this->globalVars->select_ACVWBWPERIDFGLD76KS();
		$this->toop500post_UI($groupList, $databaseRowsAsTR);
	}catch(Exception $e)
	{ 
		$this->globalVars->printException($e);
	}
} // function ends here


private function toop500post_UI($groupList, $databaseRowsAsTR)
{ 
	if (count($groupList) == 0 )
		throw new Exception('Please save some data into server first by importing manually or initiating crawling.');
?>
	<h1>Select the group id and criteria</h1>
	<form enctype="multipart/form-data" action="<?php echo get_bloginfo('wpurl') . '/wp-admin/admin.php?page=' . $this->entryPageName ?>" method="GET">
		<strong>Group-id:</strong>
		<?PHP 
		echo "<select name='show_group_id'>";
		foreach($groupList as $rows)
		{
			echo "<option value='{$rows['group_id']}'>{$rows['name']}</option>";
		}
		echo "</select>";
		?>
		<br/>
		<strong>Criteria:</strong> <br/>
		<input type="radio" name="sort_order" value="like_count" checked />Top 50 post by like count <br/>
		<input type="radio" name="sort_order" value="message_creation_time" />Newest 50 entries <br/>
		<input type="radio" name="sort_order" value="all" />All posts <br/>
		<input type="radio" name="sort_order" id='sort_order' value="weekly" />Week: 
		<input type='text' onfocus="select();jQuery('#sort_order').attr('checked','checked');" size=3 name='weekNo' value='-1' />
		<br/>
		
		<input type='hidden' name='page' value='<?PHP echo $this->entryPageName; ?>' />
		<br/><br/>
		<input type="submit" class="submit button button-primary" value="Submit">
	</form>
	<script type='text/javascript' src='//datejs.googlecode.com/files/date.js'></script>
	<script type='text/javascript'>
	(function ($){
		$("#click_me_for_date").click(function(){
			$inputButton= jQuery("#given_epoc");
			$inputButton.val( Date.parse($inputButton.val()));
			var epoc= new Date($inputButton.val()).getTime();
			//alert(epoc);
			$("#given_epoc_hidden_value").val(epoc);
			return false;
		});
	})(jQuery);
	</script>	
	<hr/>
	<h1>Entries from database</h1>
	<div style='width:600px;'>
	<table id="post_table" class='display'>
    <thead>
        <tr>
			<th>Written by</th>
			<th>Content</th>
			<th>Like #</th>
			<th>Comment #</th>
			<th>Permalink</th>
        </tr>
    </thead>
    <tbody>
        <?PHP echo $databaseRowsAsTR; ?>
    </tbody>
	</table>
	</div>
	<script type="text/javascript" language="javascript" src="//ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>
	<style type="text/css">@import url("//ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css");</style>
	
	<script type="text/javascript" charset="utf-8">
		(function($) {
			$('#post_table').dataTable({
				"aaSorting": [[ Math.floor(Math.random() * (4 - 2 + 1) + 2), "desc" ]]
			});;
		})(jQuery);
	</script>

<?PHP }

private function getWeeklyDataFromDatabase($group_id,$weekNum)
{
	$databaseRows = $this->globalVars->select_XCNVWKERSDLMC485DK($group_id,$weekNum);
	$databaseRowsAsTR = "";
	
	if( count($databaseRows) > 0)
	{
		foreach ($databaseRows as $rows)
		{
			$databaseRowsAsTR .= '<tr>';
			$databaseRowsAsTR .= "<td>{$rows['from_name']}</td>";
			$databaseRowsAsTR .= "<td>". htmlspecialchars($rows['message']) . "</td>";
			$databaseRowsAsTR .= "<td>{$rows['like_count']}</td>";
			$databaseRowsAsTR .= "<td>{$rows['comment_count']}</td>";
			$databaseRowsAsTR .= "<td><a target='_blank' href='{$rows['message_permalink']}'>{$rows['message_creation_time']}</a></td>";
			$databaseRowsAsTR .='</tr>';
		}
		//print_r($databaseRows);
	}
	return $databaseRowsAsTR;
}

private function showGroupInformationListFromDatabase($group_id,$sort_order)
{
	$databaseRows = $this->globalVars->select_KCVPWEFCMV495KL($group_id,$sort_order);
	$databaseRowsAsTR = "";
	
	if( count($databaseRows) > 0)
	{
		foreach ($databaseRows as $rows)
		{
			$databaseRowsAsTR .= '<tr>';
			$databaseRowsAsTR .= "<td>{$rows['from_name']}</td>";
			$databaseRowsAsTR .= "<td>". htmlspecialchars($rows['message']) . "</td>";
			$databaseRowsAsTR .= "<td>{$rows['like_count']}</td>";
			$databaseRowsAsTR .= "<td>{$rows['comment_count']}</td>";
			$databaseRowsAsTR .= "<td><a target='_blank' href='{$rows['message_permalink']}'>{$rows['message_creation_time']}</a></td>";
			$databaseRowsAsTR .='</tr>';
		}
		//print_r($databaseRows);
	}
	return $databaseRowsAsTR;
}

} // class ends here
?>